package cn.zc.auth.service.service.impl;

import cn.tools.core.exception.BizException;
import cn.tools.dozer.config.DozerUtils;
import cn.zc.auth.feign.entity.Dto.StudentUserDto;
import cn.zc.auth.feign.entity.StudentUser;
import cn.zc.auth.feign.entity.VO.StudentUserVO;
import cn.zc.auth.service.mapper.StudentUserMapper;
import cn.zc.auth.service.service.StudentAndClassifyService;
import cn.zc.auth.service.service.StudentUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 学生用户表 服务实现类
 * </p>
 *
 * @author keno
 * @since 2022-02-12
 */
@Service
public class StudentUserServiceImpl extends ServiceImpl<StudentUserMapper, StudentUser> implements StudentUserService {
    @Resource
    private StudentUserMapper studentUserMapper;

    @Resource
    private StudentAndClassifyService studentAndClassifyService;

    @Resource
    private DozerUtils dozerUtils;

    /**
     * security自带的加密工具
     */
    @Resource
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    @Override
    public Map<String, Object> studentLogin(String userName, String passWord) {
        StudentUser userInfo = getUserInfoByName(userName);
        if(userInfo == null){
            throw new BizException("用户名错误");
        }

        if(!bCryptPasswordEncoder.matches(passWord,userInfo.getUserPassword())){
            throw new BizException("密码错误");
        }

        Map<String,Object> map = new HashMap<>();
        map.put(userName,userInfo.getUserName());
        return map;
    }



    @Override
    public StudentUser getUserInfoByName(String userName) {
        StudentUser userInfo  = studentUserMapper.getUserInfoByName(userName);
        return userInfo;
    }



    @Override
    public void createStudent(StudentUserDto studentUserDto) {
        //从缓存中获取验证码判断验证码是否一直，这里暂时没写咱留空
        StudentUser user = dozerUtils.map(studentUserDto, StudentUser.class);
        user.setUserPassword(bCryptPasswordEncoder.encode(user.getUserPassword()));
        boolean save = save(user);
        if(!save){
            throw new BizException("注册失败");
        }
    }
}
